package com.guoqi.bridgedemo

import android.app.Activity
import android.os.Bundle
import android.view.KeyEvent
import com.guoqi.oabridge.JSBridge
import com.guoqi.oabridge.bridgeImpl.DeviceImpl
import com.guoqi.oabridge.bridgeImpl.NetWorkImpl
import com.guoqi.oabridge.bridgeImpl.ScreenImpl
import com.guoqi.oabridge.bridgeImpl.UiImpl
import kotlinx.android.synthetic.main.activity_test.*

/**
 * @author GUOQI
 */
class WebViewTestActivity : Activity() {

    companion object {
        private val testUrl = "http://www.baidu.com"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_test)
        getTypeParam()
        //initHardwareAccelerate()
    }

    private fun getTypeParam() {
        when (intent.getIntExtra("type", 0)) {
            1 -> {
                x5WebView.loadUrl(testUrl)
            }
            2 -> {
                x5WebView.loadUrl(testUrl, true, null)
            }
            3 -> {
                x5WebView.loadUrl(testUrl, true, R.drawable.view_progress_red)
            }
            4 -> {
                JSBridge.register("ui", UiImpl::class.java)
                JSBridge.register("device", DeviceImpl::class.java)
                JSBridge.register("network", NetWorkImpl::class.java)
                JSBridge.register("screen", ScreenImpl::class.java)
                //JSBridge.register("bridgeExt", BridgeExtension::class.java)
                x5WebView.loadUrl("http://47.93.160.233:3000/page/OABridgeDemo.html")
//                x5WebView.loadUrl("file:///android_asset/index.html")

            }
        }
    }

    /**
     * 启用硬件加速
     */
    private fun initHardwareAccelerate() {
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                window
                    .setFlags(
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
                    )
            }
        } catch (e: Exception) {
        }

    }

    /**
     * 返回键监听
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return if (x5WebView != null && x5WebView.canGoBack()) {
                x5WebView.goBack()
                true
            } else {
                super.onKeyDown(keyCode, event)
            }
        }
        return super.onKeyDown(keyCode, event)
    }


    override fun onDestroy() {
        if (x5WebView != null) x5WebView.destroy()
        super.onDestroy()
    }
}